package com.zhuifeng.product.service.impl;

import java.util.*;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuifeng.common.core.utils.snow.GuuidUtil;
import com.zhuifeng.product.domain.Brand;
import com.zhuifeng.product.domain.ProductRule;
import com.zhuifeng.product.service.IBrandService;
import com.zhuifeng.product.service.IProductRuleService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.zhuifeng.product.mapper.TypeMapper;
import com.zhuifeng.product.domain.Type;
import com.zhuifeng.product.service.ITypeService;

/**
 * 类型Service业务层处理
 *
 * @author zhuifeng
 * @date 2022-08-17
 */
@Service
public class TypeServiceImpl implements ITypeService
{
    @Autowired
    private TypeMapper typeMapper;

    private final StringRedisTemplate redisTemplate;

//    private final RedissonClient redissonClient;


    public TypeServiceImpl(StringRedisTemplate redisTemplate ) {
        this.redisTemplate = redisTemplate;
//        this.redissonClient = redissonClient;, RedissonClient redissonClient
    }


    @Autowired
    private IBrandService brandService;

    @Autowired
    private IProductRuleService productRuleService;

    /**
     * 查询类型下的模板
     *
     * @return 类型
     *
     */
    @Override
    public Map<String, Object> getModuleMap(String typeId) {
        Map<String, Object> stringObjectHashMap = new HashMap<>();
        List<Brand> brands = brandService.selectBrandByTypeId(typeId);
        stringObjectHashMap.put("brands",brands);
        ProductRule productRule = productRuleService.selectProductRuleByTypeId(typeId);
        stringObjectHashMap.put("productRule",productRule);
        return stringObjectHashMap;
    }

    /**
     * 查询类型列表
     *
     * @return 类型
     */
    @Override
    public Map<String, List<Type>> getTypeMap() {
        List<Type> types = typeMapper.selectTypeList();

        return disposeTypeMap(types);
    }

    private static Map<String, List<Type>> disposeTypeMap(List<Type> types){
        Map<String, List<Type>> typeMap = new HashMap<>();

        for (Type type : types) {
            if (type.getParentId().equals("0")){
                type.setTypeList(disposeTypeList(types,type.getTypeId()));
                typeMap.put(type.getTypeName(),disposeTypeList(types,type.getTypeId()));
            }
        }

        return typeMap;
    }

    /**
     * 递归调用
     * @param types
     * @param pid
     * @return
     */
    private static List<Type> disposeTypeList(List<Type> types,String pid){
        ArrayList<Type> typeList = new ArrayList<>();
        for (Type type : types) {
            if (type.getParentId().equals(pid)){
                type.setTypeList(disposeTypeList(types,type.getTypeId()));
                typeList.add(type);
            }
        }

        if (typeList.size() == 0){
            return null;
        }

        return typeList;
    }

    @Override
    public Type selectTypeByTypeId(String typeId)
    {
        return typeMapper.selectTypeByTypeId(typeId);
    }

    /**
     * 查询类型列表
     *
     * @return 类型
     */
    @Override
    public List<Type> selectTypeList() throws InterruptedException {

//        RLock lock1 = redissonClient.getLock("zhuifeng:product:type:list");
////        先判断redis是否有typeList
//        if (redisTemplate.hasKey("typeList")) {
////            存直接取
//            System.out.println(2);
//            return JSONObject.parseArray(redisTemplate.opsForValue().get("typeList"),Type.class);
//        }
//        lock1.lock();
//        try {
////            加锁
//
//            if (lock1.tryLock(300, 30, TimeUnit.SECONDS)) {
//                System.out.println("上锁");
                if (!redisTemplate.hasKey("typeList")) {
                    Random random = new Random();
                    int i = 25 + random.nextInt(6);
                    List<Type> types = typeMapper.selectTypeList();
                    redisTemplate.opsForValue().set("typeList", JSON.toJSONString(types), i, TimeUnit.MINUTES);
                    System.out.println("数据库");
                }
//            }
            return JSONObject.parseArray(redisTemplate.opsForValue().get("typeList"),Type.class);
//        }catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            System.out.println("释放锁");
//            lock1.unlock();
//        }
//        return this.selectTypeList();
    }

    /**
     * 新增类型
     *
     * @param type 类型
     * @return 结果
     */
    @Override
    public int insertType(Type type)
    {
        type.setTypeId(GuuidUtil.getUUID()+"");
        return typeMapper.insertType(type);
    }

    /**
     * 修改类型
     *
     * @param type 类型
     * @return 结果
     */
    @Override
    public int updateType(Type type)
    {
        return typeMapper.updateType(type);
    }

    /**
     * 批量删除类型
     *
     * @param typeIds 需要删除的类型主键
     * @return 结果
     */
    @Override
    public int deleteTypeByTypeIds(String[] typeIds)
    {
        return typeMapper.deleteTypeByTypeIds(typeIds);
    }

    /**
     * 删除类型信息
     *
     * @param typeId 类型主键
     * @return 结果
     */
    @Override
    public int deleteTypeByTypeId(String typeId)
    {
        return typeMapper.deleteTypeByTypeId(typeId);
    }

}
